
class RankSelection implements Selection {
	double mutationProb, crossoverProb;
	
	
	public RankSelection(double mutationProb,double crossoverProb){
		this.mutationProb=mutationProb;
		this.crossoverProb=crossoverProb;
		
	}
	public Individual reproduce(Individual[]pop){
		double ezer=Math.random();
		Individual div1;
		Individual div2;
		Individual ans;
		if(ezer<=this.crossoverProb){
			div1=select(pop);
			div2=select(pop);
			ans=div1.crossover(div2);
		}
		else 
			ans=select(pop);
		ezer=Math.random();
		if (ezer<=mutationProb)
			ans=ans.mutate();
		return ans;				
				
	}
	private Individual select(Individual[]pop){
		double ezer=Math.random();
		int i=1;
		int n=pop.length;
		double nd=n;
		double formula=(2*(nd-i))/(nd*(nd-1));
		
		while(i<n&ezer>=formula){
			i=i+1;
			formula=formula+((2*(nd-i))/(nd*(nd-1)));
				
		}
		return pop[i-1];
	}
}
